//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "GaussianRandomDefaultModel.h"
using namespace Cephei::QL::Experimental::Credit;
#include <gen/QL/Experimental/Credit/Pool.h>
#include <gen/QL/Experimental/Credit/DefaultProbKey.h>
#include <gen/QL/Experimental/Credit/OneFactorCopula.h>
#include <gen/QL/Experimental/Credit/RandomDefaultModel.h>
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Credit::CGaussianRandomDefaultModel::CGaussianRandomDefaultModel (boost::shared_ptr<QuantLib::GaussianRandomDefaultModel>& childNative, Object^ owner) : CRandomDefaultModel(CGaussianRandomDefaultModel::typeid)
{
#ifdef HANDLE
	_phGaussianRandomDefaultModel = NULL;
#endif
	_ppGaussianRandomDefaultModel = &childNative;
    _ppRandomDefaultModel = new boost::shared_ptr<QuantLib::RandomDefaultModel> (boost::dynamic_pointer_cast<QuantLib::RandomDefaultModel> (*_ppGaussianRandomDefaultModel));
}
Cephei::QL::Experimental::Credit::CGaussianRandomDefaultModel::CGaussianRandomDefaultModel (QuantLib::GaussianRandomDefaultModel& childNative, Object^ owner) : CRandomDefaultModel(CGaussianRandomDefaultModel::typeid)
{
#ifdef HANDLE
	_phGaussianRandomDefaultModel = NULL;
#endif
	_ppGaussianRandomDefaultModel = new boost::shared_ptr<QuantLib::GaussianRandomDefaultModel> (&childNative);
    _ppRandomDefaultModel = new boost::shared_ptr<QuantLib::RandomDefaultModel> (boost::dynamic_pointer_cast<QuantLib::RandomDefaultModel> (*_ppGaussianRandomDefaultModel));
    _GaussianRandomDefaultModelOwner = owner;
    _RandomDefaultModelOwner = owner;
}

Cephei::QL::Experimental::Credit::CGaussianRandomDefaultModel::CGaussianRandomDefaultModel (CGaussianRandomDefaultModel^ copy) : CRandomDefaultModel(CGaussianRandomDefaultModel::typeid)
{
#ifdef HANDLE
	_phGaussianRandomDefaultModel = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppGaussianRandomDefaultModel = new boost::shared_ptr<QuantLib::GaussianRandomDefaultModel> (copy->GetShared());
        _ppRandomDefaultModel = new boost::shared_ptr<QuantLib::RandomDefaultModel> (boost::dynamic_pointer_cast<QuantLib::RandomDefaultModel> (*_ppGaussianRandomDefaultModel));
    }
}
Cephei::QL::Experimental::Credit::CGaussianRandomDefaultModel::CGaussianRandomDefaultModel (PLATFORM::Type^ t) : CRandomDefaultModel(CGaussianRandomDefaultModel::typeid)
{
#ifdef HANDLE
	_phGaussianRandomDefaultModel = NULL;
#endif
	if (!t->IsSubclassOf(CGaussianRandomDefaultModel::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Credit::CGaussianRandomDefaultModel::CGaussianRandomDefaultModel (QuantLib::Handle<QuantLib::GaussianRandomDefaultModel>& childNative, Object^ owner)  : CRandomDefaultModel(CGaussianRandomDefaultModel::typeid)
{
	_phGaussianRandomDefaultModel = &childNative;
	_ppGaussianRandomDefaultModel = &static_cast<boost::shared_ptr<QuantLib::GaussianRandomDefaultModel>>(childNative.currentLink());
    _ppRandomDefaultModel = new boost::shared_ptr<QuantLib::RandomDefaultModel> (boost::dynamic_pointer_cast<QuantLib::RandomDefaultModel> (*_ppGaussianRandomDefaultModel));
    _GaussianRandomDefaultModelOwner = owner;
}
Cephei::QL::Experimental::Credit::CGaussianRandomDefaultModel::CGaussianRandomDefaultModel (QuantLib::Handle<QuantLib::GaussianRandomDefaultModel> childNative)  : CRandomDefaultModel(CGaussianRandomDefaultModel::typeid)
{
	_phGaussianRandomDefaultModel = &childNative;
	_ppGaussianRandomDefaultModel = &static_cast<boost::shared_ptr<QuantLib::GaussianRandomDefaultModel>>(childNative.currentLink());
    _ppRandomDefaultModel = new boost::shared_ptr<QuantLib::RandomDefaultModel> (boost::dynamic_pointer_cast<QuantLib::RandomDefaultModel> (*_ppGaussianRandomDefaultModel));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Credit::CGaussianRandomDefaultModel::CGaussianRandomDefaultModel (QuantLib::GaussianRandomDefaultModel childNative)  : CRandomDefaultModel(CGaussianRandomDefaultModel::typeid)
{
#ifdef HANDLE
	_phGaussianRandomDefaultModel = NULL;
#endif
	_ppGaussianRandomDefaultModel = new boost::shared_ptr<QuantLib::GaussianRandomDefaultModel> (new QuantLib::GaussianRandomDefaultModel (childNative));
    _ppRandomDefaultModel = new boost::shared_ptr<QuantLib::RandomDefaultModel> (boost::dynamic_pointer_cast<QuantLib::RandomDefaultModel> (*_ppGaussianRandomDefaultModel));
}
#endif

Cephei::QL::Experimental::Credit::CGaussianRandomDefaultModel::~CGaussianRandomDefaultModel ()
{
    if (_ppGaussianRandomDefaultModel != NULL)
    {
	    delete _ppGaussianRandomDefaultModel;
        _ppGaussianRandomDefaultModel = NULL;
    }
}
Cephei::QL::Experimental::Credit::CGaussianRandomDefaultModel::!CGaussianRandomDefaultModel ()
{
    if (_ppGaussianRandomDefaultModel != NULL)
    {
	    delete _ppGaussianRandomDefaultModel;
    }
}
QuantLib::GaussianRandomDefaultModel& Cephei::QL::Experimental::Credit::CGaussianRandomDefaultModel::GetReference ()
{
    if (_ppGaussianRandomDefaultModel == NULL) throw REFNEW NativeNullException ();
	return **_ppGaussianRandomDefaultModel;
}
boost::shared_ptr<QuantLib::GaussianRandomDefaultModel>& Cephei::QL::Experimental::Credit::CGaussianRandomDefaultModel::GetShared ()
{
    if (_ppGaussianRandomDefaultModel == NULL) throw REFNEW NativeNullException ();
	return *_ppGaussianRandomDefaultModel;
}
QuantLib::GaussianRandomDefaultModel* Cephei::QL::Experimental::Credit::CGaussianRandomDefaultModel::GetPointer ()
{
    if (_ppGaussianRandomDefaultModel == NULL) throw REFNEW NativeNullException ();
	return &**_ppGaussianRandomDefaultModel;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::GaussianRandomDefaultModel>& Cephei::QL::Experimental::Credit::CGaussianRandomDefaultModel::GetHandle ()
{
	if (_phGaussianRandomDefaultModel == NULL)
	{
		_phGaussianRandomDefaultModel = new Handle<QuantLib::GaussianRandomDefaultModel> (*_ppGaussianRandomDefaultModel);
	}
	return *_phGaussianRandomDefaultModel;
}
#endif
bool Cephei::QL::Experimental::Credit::CGaussianRandomDefaultModel::HasNative () 
{
	return (_ppGaussianRandomDefaultModel != NULL);
}

Cephei::QL::Experimental::Credit::IGaussianRandomDefaultModel^ Cephei::QL::Experimental::Credit::CGaussianRandomDefaultModel::NextSequence (Microsoft::FSharp::Core::FSharpOption<Double>^ tmax)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Real _tmax = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (tmax) ? (QuantLib::Real)ValueHelper::Convert (tmax->Value) : QL_MAX_REAL); //9a
    	(*_ppGaussianRandomDefaultModel)->nextSequence ( _tmax );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Credit::IGaussianRandomDefaultModel^ Cephei::QL::Experimental::Credit::CGaussianRandomDefaultModel::Reset::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppGaussianRandomDefaultModel)->reset ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

